﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Loaddata.getdebtorsector" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;
namespace SIPO.Web.AjaxHandler.Loaddata
{

    public class getdebtorsector : SIPO.Web.BaseHandler
    {

        recDebtorSectorRepo recDebtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        string MethodName = string.Empty;
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            if (context.Request.Params["method"] != null)
                MethodName = context.Request.Params["method"];

            if (MethodName == "getsubsectorbydebtor")
            {
                int debtor_id = Convert.ToInt32(context.Request.Params["debtor_id"]);
                List<recDebtorSector> debtorsectors = recDebtorSectorDal.GetGroupSubSectorList(debtor_id, base.SubSubSectorIDs);
                var result = debtorsectors; //debtorsectors.Where(x => x.DebtorID == id && x.SubSectorID == subsectorid);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
                //ambil data debtor sector dengan parameter debtor_id,sector_id,subsector_id,subsubsector_id
            else if (MethodName == "getdatabyparaminvoice")
            {
                int debtor_id = Convert.ToInt32(context.Request.Params["debtor_id"]);
                int subsubsectorid = Convert.ToInt32(context.Request.Params["subsubsector_id"]);
                int subsectorid = Convert.ToInt32(context.Request.Params["subsector_id"]);
                int sectorid = Convert.ToInt32(context.Request.Params["sector_id"]);
                List<recDebtorSector> debtorsector = recDebtorSectorDal.GetListByDebtor_Sector_SubSector_SubSubSector(debtor_id,sectorid,subsectorid,subsubsectorid);
                var result = debtorsector;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "getdatabydebtorcorporateaction")
            {
                int debtor_id = Convert.ToInt32(context.Request.Params["debtor_id"]);
                List<recDebtorSector> debtorSectors = recDebtorSectorDal.GetListByDebtorCorporateAction(debtor_id, "2", "4,5");

                //if (!string.IsNullOrEmpty(base.SubSubSectorIDs))
                //{
                //    string[] datas = base.SubSubSectorIDs.Split(',');
                //    debtorSectors = debtorSectors.Where(m => datas.Contains(m.SubSubSectorID.ToString())).ToList();
                //}
                
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(debtorSectors));
            }

            else if (MethodName == "getbyid")
            {
                int debtorsectorID = Convert.ToInt32(context.Request.Params["debtorsectorID"]);
                List<recDebtorSector> debtorsectors = recDebtorSectorDal.GetListByID(debtorsectorID);
                var result = debtorsectors; //debtorsectors.Where(x => x.DebtorID == id && x.SubSectorID == subsectorid);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
                
            else if (MethodName == "idinobjection")
            {
                int debtorID = Convert.ToInt32(context.Request.Params["debtorID"]);
                List<recDebtorSector> debtorsectors = recDebtorSectorDal.GetList();
                var result = debtorsectors.Where(x => x.DebtorID == debtorID);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
                
            else
            {
                int id = Convert.ToInt32(context.Request.Params["debtor_id"]);
                int subsectorid = Convert.ToInt32(context.Request.Params["subsector_id"]);
                List<recDebtorSector> debtorsectors = recDebtorSectorDal.GetGroupSubSectorList(id, subsectorid, base.SubSubSectorIDs);
                var result = debtorsectors;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}